package cn.lcfms.app.admin.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;

import cn.lcfms.app.BaseController;
import cn.lcfms.bin.App;
import cn.lcfms.bin.BaseService;
import cn.lcfms.bin.CommonAddView;
import cn.lcfms.bin.CommonEditView;
import cn.lcfms.bin.CommonPageView;

public class AdminBaseController extends BaseController{
    public static List<HashMap<String, Object>> itemcache=null;//用于缓存所有的栏目列表
	/**
	 * 该方法将在执行控制器之前被执行
	 * @throws Exception 
	 */
    @Override
    protected void beforeProtected(HttpServletRequest request,HttpServletResponse response){
    	super.beforeProtected(request,response);
    	if(itemcache==null){
    		set_item_cache();
    	}
    }  
    /**
   	 * 该方法将在执行控制器之后，调入模板之前被执行
     * @throws IOException 
   	 */
    @Override
    protected void afterProtected(HttpServletRequest request,HttpServletResponse response,ModelAndView view) throws IOException{  	
    	super.afterProtected(request,response,view);
    	String basePath=(String) request.getAttribute("APP");
    	request.setAttribute("JS", basePath+"statics/tag/js/"); 
    	request.setAttribute("CSS", basePath+"statics/tag/css/"); 
    	request.setAttribute("IMG", basePath+"statics/tag/images/");   			
    	if(view!=null && view.getViewName()!=null){	
    		String viewname=view.getViewName();	
    		if(viewname.startsWith("local:")){
    			view.setViewName("admin/local");
    			view.addObject("LOCAL", viewname.substring(6));	
    		}
			if(viewname.equals("admin/table/selectpage")){
				Map<String, Object> model = view.getModel();
				CommonPageView view2=(CommonPageView) model.get("CommonPageView");
				Method method;
				try {
					method = CommonPageView.class.getDeclaredMethod("setSelectPage", HttpServletRequest.class);
					method.setAccessible(true);
					method.invoke(view2, request);
				} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
					e.printStackTrace();
				}
			}
			if(viewname.equals("admin/table/addpage")){
				Map<String, Object> model = view.getModel();
				CommonAddView view2=(CommonAddView) model.get("CommonAddView");
				Method method;
				try {
					method = CommonAddView.class.getDeclaredMethod("setAddPage", HttpServletRequest.class);
					method.setAccessible(true);
					method.invoke(view2, request);
				} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
					e.printStackTrace();
				}
			}
			if(viewname.equals("admin/table/editpage")){
				Map<String, Object> model = view.getModel();
				CommonEditView view2=(CommonEditView) model.get("CommonEditView");
				Method method;
				try {
					method = CommonEditView.class.getDeclaredMethod("setEditPage", HttpServletRequest.class);
					method.setAccessible(true);
					method.invoke(view2, request);
				} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
					e.printStackTrace();
				}
			}
		}
    }
    /**
	 * 初始化所有栏目
	 * @return
	 * @throws Exception
	 */
	protected void set_item_cache(){
		BaseService service = App.getService("item");
		List<HashMap<String, Object>> list=service.order("sort").selectList();
		for(int i=0;i<list.size();i++){
			long itemId=(long) list.get(i).get("itemId");
			for(int m=0;m<list.size();m++){
				int parentId=(int) list.get(m).get("parentId");
				if(parentId==itemId){
					list.get(i).put("hasChild", true);
					break;
				}
			}
		}		
		itemcache=list;	
	}
}
